/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  HireRelativeActivity.java   
 * @Package com.jiyi.activity.new_maintenance   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-9-7 上午10:09:38   
 * @version V1.0     
 */
package com.jiyi.activity.new_maintenance;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jiyi.adapter.recyclerview.CommonAdapter;
import com.jiyi.adapter.recyclerview.base.ViewHolder;
import com.jiyi.adapter.recyclerview.wrapper.EmptyWrapper;
import com.jiyi.bean.maintenance.HireUnitBean;
import com.jiyi.common.StringInterface;
import com.jiyi.common.VolleyRequest;
import com.jiyi.easemob.util.UserHelp;
import com.jiyi.inteface.WebApi;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppBase;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: HireRelativeActivity
 * @Description:TODO(新雇佣关系)
 * @author: HYM
 * @date: 2017-9-7 上午10:09:38
 * 
 */
public class NewHireRelativeActivity extends ActivityBase implements
		OnRefreshListener {

	/* 头部 */
	@BindView(R.id.tvBack)
	TextView tvBack;
	@BindView(R.id.tvTitle)
	TextView tvTitle;
	@BindView(R.id.tvSearch)
	TextView tvSearch;

	@BindView(R.id.mainList)
	RecyclerView mainList;
	private List<HireUnitBean> dataList = new ArrayList<HireUnitBean>();
	EmptyWrapper adapter;

	// 加载进度条
	Dialog dialog;
	LayoutInflater inflater;

	// 保存当前选择的id
	ArrayList<String> idsList = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_hire_relative);
		ButterKnife.bind(this);
		init();
	}

	@Override
	protected void onDestroy() {
		hideDialog(dialog);
		super.onDestroy();
	}

	private void init() {
		inflater = LayoutInflater.from(this);
		setTop();
		setAdapter();
		getData();
	}

	private void setTop() {
		tvTitle.setText("新增雇佣关系");
		tvSearch.setVisibility(View.GONE);
	}

	private void setAdapter() {
		// 设置布局管理器
		mainList.setLayoutManager(new LinearLayoutManager(this));
		// 初始化适配器
		CommonAdapter commonAdapter = new CommonAdapter<HireUnitBean>(this,
				R.layout.item_hire_relative, dataList) {
			@Override
			protected void convert(final ViewHolder holder,
					final HireUnitBean t, final int position) {
				holder.setText(R.id.tvOwnerName, t.getCompany_name());
				holder.setOnClickListener(R.id.rlRoot, new OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent intent = new Intent();
						intent.putExtra("companyId", t.getCompany_id());
						intent.putExtra("ownerName", t.getCompany_name());
						setResult(RESULT_OK, intent);
						finish();
					}
				});
			}
		};

		adapter = new EmptyWrapper(commonAdapter);
		View emptyView = inflater.inflate(R.layout.empty_view_text_and_image,
				mainList, false);
		ImageView ivEmptyTip = (ImageView) emptyView
				.findViewById(R.id.ivEmptyTip);
		TextView tvEmptyTip = (TextView) emptyView
				.findViewById(R.id.tvEmptyTip);
		ivEmptyTip.setImageResource(R.mipmap.tip_empty_huodong);
		tvEmptyTip.setText(R.string.NO_DATA);
		adapter.setEmptyView(emptyView);

		// recycleview设置适配器
		mainList.setAdapter(adapter);
	}

	private void getData() {

		String url;
		
		if (UserInfoUtil.getCurrentUserRole() == AppConfig.PROJECT_MNG) {
			url = WebApi.OWNER_HIRE_WB_LIST;
		}
		else {
			url = WebApi.MAINTENANCE_NO_HIRE_COMPANY_LIST;
		}
		
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN,
				UserInfoUtil.getCurrentUserAccessToken());

		dialog = showDialog(dialog);
		VolleyRequest.StringRequestPost(this, url, "", params,
				new StringInterface(this) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								String data = obj.getString("data");
								List<HireUnitBean> temp = new Gson().fromJson(
										data,
										new TypeToken<List<HireUnitBean>>() {
										}.getType());
								dataList.clear();
								dataList.addAll(temp);
								adapter.notifyDataSetChanged();
							} else if (status.equals("404")
									&& info.contains("尚未登录")) {
								ToastUtil.getInstances().showMsg(
										"尚未登录或该用户在其他地方登录，请重新登录！");
								// 清除环信登录
								UserHelp.getInstance().clearUser(
										AppBase.Context, true);
								return;
							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
							hideDialog(dialog);
						}
					}

					@Override
					public void stringError(VolleyError error) {
						hideDialog(dialog);
						ToastUtil.getInstances().showMsg(
								R.string.err_http_response);
					}
				});

	}

	@Override
	public void onRefresh() {
		getData();
	}

	@OnClick({ R.id.tvBack })
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.tvBack:
			finish();
			break;
		default:
			break;
		}
	}
}
